﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;

namespace iEN.Alarm
{
    public partial class SetAlarm : System.Web.UI.Page
    {
        string strAccount = "";
        string strIP = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            strAccount = Session["LoginAccount"].ToString();
            strIP = Session["LoginIP"].ToString();

            if (Session["SysAdmin"].ToString() != "2")
            {
                UpdatePanel4.Visible = false;
                TabContainer1.Visible = false;
                lblMsg.Visible = true;
            }

            if (!Page.IsPostBack)
            {
                Session["_parent_id"] = "";
                initialAlarmSetting();
            }
        }

        protected void initialAlarmSetting()
        {
            DataTable dtBuilding = new DBAccess().getAlarmBuildingTable();
            ddlAlarmBuilding.DataSource = dtBuilding;
            ddlAlarmBuilding.DataTextField = "_name";
            ddlAlarmBuilding.DataValueField = "_id";
            ddlAlarmBuilding.DataBind();

            ddlAlarmBuilding2.DataSource = dtBuilding;
            ddlAlarmBuilding2.DataTextField = "_name";
            ddlAlarmBuilding2.DataValueField = "_id";
            ddlAlarmBuilding2.DataBind();

            DataTable dtDormitory = new DataTable();
            ddlAlarmDormitory.DataSource = dtDormitory;
            ddlAlarmDormitory.DataBind();

            DataTable dtGrp = new DBAccess().getAlarmGrpTable();
            ddlAlarmGrp.DataSource = dtGrp;
            ddlAlarmGrp.DataTextField = "_name";
            ddlAlarmGrp.DataValueField = "_id";
            ddlAlarmGrp.DataBind();



        }

        protected void imgTip_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel2.Visible = !this.Panel2.Visible;
            if (Panel2.Visible == true)
                imgTip.ImageUrl = "~/images/info_hide.gif";
            else
                imgTip.ImageUrl = "~/images/info_show.gif";
        }

        #region protected void gvAlarmBuilding_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvAlarmBuilding_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
        }
        #endregion

        #region protected void gvAlarmBuilding_RowUpdated(object sender, GridViewUpdateEventArgs e)
        protected void gvAlarmBuilding_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            new DBAccess().addEventLog(strAccount, strIP, 81, "宿舍大樓名稱：" + e.OldValues[0].ToString() + "/用電基準值：" + e.NewValues[0].ToString(), "1");
        }
        #endregion

        #region protected void gvAlarmDormitory_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvAlarmDormitory_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }
        #endregion

        #region protected void gvAlarmDormitory_RowUpdating(object sender, GridViewUpdateEventArgs e)
        protected void gvAlarmDormitory_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int iIndex = e.RowIndex;
            CheckBox cbxEnabled = default(CheckBox);

            cbxEnabled = (CheckBox)gvAlarmDormitory.Rows[iIndex].Cells[1].FindControl("CheckBox1");
            if (cbxEnabled.Checked)
                Session["_enabled"] = 1;
            else
                Session["_enabled"] = 0;
        }
        #endregion

        #region protected void gvAlarmDormitory_RowUpdated(object sender, GridViewUpdateEventArgs e)
        protected void gvAlarmDormitory_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            new DBAccess().addEventLog(strAccount, strIP, 82, "寢室名稱：" + e.OldValues[0].ToString() + "/告警值：" + e.NewValues[0].ToString(), "1");
        }
        #endregion

        #region protected void ddlAlarmBuilding_DataBound(object sender, EventArgs e) //宿舍大樓下拉選單加入第一行
        protected void ddlAlarmBuilding_DataBound(object sender, EventArgs e)
        {
            ddlAlarmBuilding.Items.Insert(0, new ListItem("----", ""));
        }
        #endregion

        #region protected void ddlAlarmBuilding_SelectedIndexChanged(object sender, EventArgs e) //宿舍大樓選項改變
        protected void ddlAlarmBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (ddlAlarmBuilding.SelectedIndex == 0)
                Session["_parent_id"] = "";
            else
                Session["_parent_id"] = ddlAlarmBuilding.SelectedValue.ToString();

        }
        #endregion

        #region protected void ddlAlarmBuilding2_DataBound(object sender, EventArgs e) //宿舍大樓下拉選單加入第一行
        protected void ddlAlarmBuilding2_DataBound(object sender, EventArgs e)
        {
            ddlAlarmBuilding2.Items.Insert(0, new ListItem("----", ""));
        }
        #endregion

        #region protected void ddlAlarmBuilding2_SelectedIndexChanged(object sender, EventArgs e) //宿舍大樓選項改變
        protected void ddlAlarmBuilding2_SelectedIndexChanged(object sender, EventArgs e)
        {

            DataTable dtDormitory = new DataTable();
            

            if (ddlAlarmBuilding2.SelectedIndex == 0)
            {
                ddlAlarmDormitory.DataSource = dtDormitory;
                ddlAlarmDormitory.DataBind();
            }
            else
            {
                dtDormitory = new DBAccess().getAlarmDormitoryTable(int.Parse(ddlAlarmBuilding2.SelectedValue));
                ddlAlarmDormitory.DataSource = dtDormitory;
                ddlAlarmDormitory.DataTextField = "_name";
                ddlAlarmDormitory.DataValueField = "_dormitory_id";
                ddlAlarmDormitory.DataBind();
            }
        }
        #endregion

        #region protected void ddlAlarmDormitory_DataBound(object sender, EventArgs e) //告警寢室下拉選單加入第一行
        protected void ddlAlarmDormitory_DataBound(object sender, EventArgs e)
        {
            ddlAlarmDormitory.Items.Insert(0, new ListItem("----", ""));
        }
        #endregion

        #region protected void ddlAlarmDormitory_SelectedIndexChanged(object sender, EventArgs e) //告警寢室選項改變
        protected void ddlAlarmDormitory_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmDormitoryGrpTable(ddlAlarmDormitory.SelectedValue);
            gvAlarmDormitoryGrp.DataSource = dt;
            gvAlarmDormitoryGrp.DataBind();
            Session["AlarmDormitoryGrp"] = dt;
        }
        #endregion

        #region protected void ddlAlarmGrp_DataBound(object sender, EventArgs e) //告警群組下拉選單加入第一行
        protected void ddlAlarmGrp_DataBound(object sender, EventArgs e)
        {
            ddlAlarmGrp.Items.Insert(0, new ListItem("----", ""));
        }
        #endregion

        #region protected void ddlAlarmGrp_SelectedIndexChanged(object sender, EventArgs e) //告警群組選項改變
        protected void ddlAlarmGrp_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        #endregion

        #region protected void gvAlarmDormitoryGrp_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvAlarmDormitoryGrp_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            DataTable dtAlarmDormitoryGrp = (DataTable)Session["AlarmDormitoryGrp"];
            int iIndex = Convert.ToInt32(e.CommandArgument);
            string DormitoryID = dtAlarmDormitoryGrp.Rows[iIndex][0].ToString();
            string GrpID = dtAlarmDormitoryGrp.Rows[iIndex][1].ToString();
            string DormitoryName = dtAlarmDormitoryGrp.Rows[iIndex][4].ToString();
            string GrpName = dtAlarmDormitoryGrp.Rows[iIndex][7].ToString();

            //取得第幾列被選取 
            Boolean flag;

            if (e.CommandName == "Del")
            {
                flag = new DBAccess().deleteAlarmDormitoryGrp(DormitoryID, GrpID);

                if (flag == true)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 84, "寢室名稱：" + DormitoryName + "/告警群組名稱：" + GrpName, "1");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員刪除成功')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 84, "寢室名稱：" + DormitoryName + "/告警群組名稱：" + GrpName, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('告警人員刪除失敗')", true);
                }
                ddlAlarmDormitory_SelectedIndexChanged(null, null);
            }
        }
        #endregion

        #region protected void btnSet_Click(object sender, EventArgs e) //新增寢室告警群組
        protected void btnSet_Click(object sender, EventArgs e)
        {
            DataTable dt = new DBAccess().getAlarmDormitoryGrpTable();
            DataTable dtDormitoryGrp;
            dt.DefaultView.RowFilter = " _dormitory_id = '" + ddlAlarmDormitory.SelectedValue + "' AND _group_id ='" + ddlAlarmGrp.SelectedValue + "'";
            dtDormitoryGrp = dt.DefaultView.ToTable();
            if (dtDormitoryGrp != null)
            {
                if (dtDormitoryGrp.Rows.Count > 0)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 83, "寢室名稱：" + ddlAlarmDormitory.SelectedItem.Text + "/告警群組名稱：" + ddlAlarmGrp.SelectedItem.Text, "0");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('寢室告警群組已重複設定, 請重新設定寢室告警群組')", true);
                    return;
                }
            }

            bool addSuc = new DBAccess().addAlarmDormitoryGrp(ddlAlarmDormitory.SelectedValue, ddlAlarmGrp.SelectedValue);

            if (addSuc == true)
            {
                new DBAccess().addEventLog(strAccount, strIP, 83, "寢室名稱：" + ddlAlarmDormitory.SelectedItem.Text + "/告警群組名稱：" + ddlAlarmGrp.SelectedItem.Text, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增成功')", true);
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 83, "寢室名稱：" + ddlAlarmDormitory.SelectedItem.Text + "/告警群組名稱：" + ddlAlarmGrp.SelectedItem.Text, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
            }

            ddlAlarmDormitory_SelectedIndexChanged(null, null);

        }
        #endregion
    }
}